//
//  LZMyVideoAndRestaurantsBaseScrollView.m
//  laziz_Consumer
//
//  Created by xbn_iMac on 2016/11/15.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZMyVideoAndRestaurantsBaseScrollView.h"
#import "UIScrollView+LZContentOffset.h"
#import "LZMyRecordVideoTableView.h"
#import "LZMyRecordRestaurantsTableView.h"
#import "LZMyReviewedTableView.h"
#import "Masonry.h"

@interface LZMyVideoAndRestaurantsBaseScrollView ()<UIScrollViewDelegate>

@property (nonatomic ,assign) MyRecordsType type;
@property (nonatomic ,assign) NSInteger selectedButtonTag;

@end

@implementation LZMyVideoAndRestaurantsBaseScrollView

- (instancetype) initWithFrame:(CGRect)frame andType:(MyRecordsType)type {
    self = [super initWithFrame:frame];
    if (self) {
        self.contentSize=CGSizeMake(SCREEN_WIDTH*2, 0);
        self.delegate=self;
        self.showsHorizontalScrollIndicator=NO;
        self.pagingEnabled=YES;
        self.scrollEnabled = NO;
        self.bounces=NO;
        self.type = type;
        [self configSubTableViews];
    }
    return self;
}

- (void)configSubTableViews {
    UIView *container = [[UIView alloc] init];
    [self addSubview:container];
    
    [container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
        make.height.equalTo(self);
    }];
    
    if (self.type == MyRecordsType_Reviewed) {
        [self setReviewedListUI:container];
    }
    else {
        [self setNotReviewedListUI:container];
    }
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    [self setLanguageContentOffset];
}

#pragma mark - 根据类型设置子View的UI
// 评论列表的UI
- (void)setReviewedListUI:(UIView*)aView {
    [aView addSubview:self.reviewedVideoTable];
    [aView addSubview:self.reviewedRestaurantsTable];
    
    [self.reviewedVideoTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.bottom.equalTo(aView);
        make.leading.mas_equalTo(0);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    
    [self.reviewedRestaurantsTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.bottom.equalTo(aView);
        make.leading.equalTo(self.reviewedVideoTable.mas_trailing);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    
    [aView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.reviewedRestaurantsTable.mas_trailing);
    }];
}

// 非评论列表的UI
- (void)setNotReviewedListUI:(UIView*)aView {

    [aView addSubview:self.videoTable];
    
    [self.videoTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.bottom.equalTo(aView);
        make.leading.mas_equalTo(0);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    
    if (self.type != MyRecordsType_ToBeReviewed) {
        [aView addSubview:self.restaurantsTable];
        
        [self.restaurantsTable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.and.bottom.equalTo(aView);
            make.leading.equalTo(self.videoTable.mas_trailing);
            make.width.mas_equalTo(SCREEN_WIDTH);
        }];
    }
    
    [aView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (self.type == MyRecordsType_ToBeReviewed) {
            make.trailing.equalTo(self.videoTable.mas_trailing);
        }
        else {
            make.trailing.equalTo(self.restaurantsTable.mas_trailing);
        }
    }];
}

// 视频列表
- (LZMyRecordVideoTableView*)videoTable {
    if (_videoTable) {
        return _videoTable;
    }
    
    LZMyRecordVideoTableView *table = [[LZMyRecordVideoTableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    table.tag = 10;
    table.type = self.type;
    _videoTable = table;
    return _videoTable;
}

// 餐厅列表
- (LZMyRecordRestaurantsTableView*)restaurantsTable {
    if (_restaurantsTable) {
        return _restaurantsTable;
    }
    
    LZMyRecordRestaurantsTableView *table = [[LZMyRecordRestaurantsTableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    table.tag = 11;
    table.type = self.type;
    _restaurantsTable = table;
    return _restaurantsTable;
}

// 视频的评论列表
- (LZMyReviewedTableView *)reviewedVideoTable {
    if (_reviewedVideoTable) {
        return _reviewedVideoTable;
    }
    
    LZMyReviewedTableView *table = [[LZMyReviewedTableView alloc] initWithType:ReviewType_Video];
    table.tag = 10;
    _reviewedVideoTable = table;
    return _reviewedVideoTable;
}

// 餐厅的评论列表
- (LZMyReviewedTableView *)reviewedRestaurantsTable {
    if (_reviewedRestaurantsTable) {
        return _reviewedRestaurantsTable;
    }
    
    LZMyReviewedTableView *table = [[LZMyReviewedTableView alloc] initWithType:ReviewType_Restaurants];
    table.tag = 11;
    _reviewedRestaurantsTable = table;
    return _reviewedRestaurantsTable;
}

#pragma mark - Public Method

- (void)clickButtonTag:(NSInteger)tag {
    self.selectedButtonTag = tag;
    if (self.type != MyRecordsType_Reviewed) {
        if (tag == 0) {
            [self.videoTable startPlayVideo];
        }
        else {
            [self.videoTable stopPlayVideo];
        }
    }
    LZMyVideoAndRestaurantsBaseTableView *table = [self viewWithTag:tag+10];
    [table checkHaveData];
    [self setContentOffset:CGPointMake(table.frame.origin.x, 0)];
    
}

// 页面展示的处理方法
- (void)viewShow {
    if (self.selectedButtonTag == 0 && self.type != MyRecordsType_Reviewed) {
        [self.videoTable startPlayVideo];
    }
    
    LZMyVideoAndRestaurantsBaseTableView *table = [self viewWithTag:self.selectedButtonTag+10];
    [table checkHaveData];
    
}

// 页面消失的处理方法
- (void)viewDiss {
    if (self.selectedButtonTag == 0 && self.type != MyRecordsType_Reviewed) {
        [self.videoTable stopPlayVideo];
    }
}

@end
